package au.id.weston.scott.Vincent;

// Copyright (c) 2009, Scott Weston <scott@weston.id.au>
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice,
//       this list of conditions and the following disclaimer in the documentation
//       and/or other materials provided with the distribution.
//     * Neither the name of Scott Weston nor the names of contributors may be
//       used to endorse or promote products derived from this software without
//       specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

class DatabaseHelper extends SQLiteOpenHelper {
	static final String TAG = "sambasync/DatabaseHelper";
	static final String DATABASE_NAME = "sambasync.db";
	static final int DATABASE_VERSION = 2;
	static final String MOUNT_TABLE = "mounts";
	static final String BSSID_TABLE = "bssid";

	SQLiteDatabase db;

	DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		db = getWritableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "DatabaseHelper onCreate called");
		db.execSQL("CREATE TABLE " + MOUNT_TABLE + " ("
				+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ "mount_name TEXT,"
				+ "username TEXT,"
				+ "password TEXT,"
				+ "server TEXT,"
				+ "share TEXT,"
				+ "directory TEXT,"
				+ "destination TEXT,"
				+ "direction INTEGER,"
				+ "last_used DATE"
				+ ");");
		db.execSQL("CREATE TABLE " + BSSID_TABLE + " ("
				+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ "bssid TEXT,"
				+ "mount_id INTEGER,"
				+ "last_run TIMESTAMP default CURRENT_TIMESTAMP"
				+ ");");
	}

	@Override
	public synchronized void close() {
		db.close();
		super.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "database upgrade requested, ignored.");
		
		if (oldVersion == 1 && newVersion == 2) {
			db.execSQL("CREATE TABLE " + BSSID_TABLE + " ("
					+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
					+ "bssid TEXT,"
					+ "mount_id INTEGER,"
					+ "last_run TIMESTAMP default CURRENT_TIMESTAMP"
					+ ");");
		}
	}
	
	public void insertNewConnection(ContentValues values) {
		Log.d(TAG, "insertNewConnection: " + values);
		Long rowid = db.insert(MOUNT_TABLE, "", values);
		if (rowid < 0) {
			Log.e(TAG, "database insert failed: " + rowid);
		} else {
			Log.d(TAG, "database insert success, rowid=" + rowid);
		}
	}

//	public void insertLocationData(ContentValues values) {
//		Log.d(TAG, "insertLocationData: " + values);
//
//		try {
//			Long rowid = db.insert(DatabaseHelper.TABLE, "locations", values);
//			if (rowid < 0) {
//				Log.e(TAG, "database insert failed: " + rowid);
//			} else {
//				Log.d(TAG, "sample collected, rowid=" + rowid);
//			}
//		} catch (Exception e) {
//			Log.e(TAG, "database exception: " + e);
//		}
//	}
//
	public int getCount(Cursor c) {
		Log.d(TAG, "getCount()");

		try {
			Log.d(TAG, "count=" + c.getCount());
			return c.getCount();
		} catch (Exception e) {
			Log.e(TAG, "database exception: " + e);
			return -1;
		}
	}

	public Cursor queryAll() {
		Log.d(TAG, "queryAll()");
		
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(DatabaseHelper.MOUNT_TABLE);
		
		return qb.query(db, null, null, null, null, null, null);
	}
	
	public Cursor getMountById(int mount_id) {
		Log.d(TAG, "getMountById()");
		
		SQLiteQueryBuilder mqb = new SQLiteQueryBuilder();
		mqb.setTables(DatabaseHelper.MOUNT_TABLE);
		
		String proj = String.format("id=%d", mount_id);
		return mqb.query(db, null, proj, null, null, null, null);
	}
}
